import java.util.*;

public class AlgoLuhn {
    public static void main(String args[]) {
        List<Integer> cardNumber = Arrays.asList(4, 5, 6, 1, 2, 6, 1, 2, 1, 2, 3, 4, 5, 4, 6, 7);
        AlgoLuhn algoLuhn = new AlgoLuhn();
        boolean result = algoLuhn.checkLuhnAlgorythm(cardNumber);
        System.out.println("Card is valid " + result);
    }
    protected boolean checkLuhnAlgorythm(List<Integer> cardNumber) {
        int sum = 0;
        int temp;
        List<Integer> cardNumberTemp = new ArrayList<Integer>();
        for (int i = 0; i < cardNumber.size(); i++) {
            temp = cardNumber.get(i);
            if (i % 2 == 0) {
                if (2 * temp > 9) {
                    cardNumberTemp.add(i, (2 * temp - 9));
                } else {
                    cardNumberTemp.add(i, 2 * temp);
                }
            } else {
                cardNumberTemp.add(i, temp);
            }
            sum = sum + cardNumberTemp.get(i);
        }
        System.out.println(sum);
        return (sum % 10 == 0);  //To change body of created methods use File | Settings | File Templates.
    }
}
